package com.itheima.reggie.controller;

import cn.hutool.core.util.RandomUtil;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.common.SmsTemplate;
import com.itheima.reggie.domain.User;
import com.itheima.reggie.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
public class UserController {

    @Autowired
    private SmsTemplate smsTemplate;

    @Autowired
    private HttpSession session;

    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate redisTemplate;

    //发送验证码
    @PostMapping("/user/sendMsg")
    public ResultInfo sendSmsCode(@RequestBody Map<String, String> map) {

        //1.接收参数
        String phone = map.get("phone");
        //2.生成验证码
        //String code = RandomUtil.randomNumbers(6); //todo 为了测试方便,临时写死
        String code = "123";

        //3. 调用阿里云服务把验证吗发送出去
        //smsTemplate.sendSms(phone, code); //todo 为了省钱 暂时注释
        log.info("发送的验证码是:" + code);

        //4. 将验证码保存到session,等候检验
        //session.setAttribute("SMS_CODE", code);

        //========================将生成好的验证码保存到redis中，并设置5分钟有效期=======================//
        redisTemplate.opsForValue().set("SMS_CODE" + phone, code, 5, TimeUnit.MINUTES);

        //5. 返回结果
        return ResultInfo.success(null);
    }

    //注册登录
    @PostMapping("/user/login")
    public ResultInfo login(@RequestBody Map<String, String> map) {
        //1.接收参数
        String phone = map.get("phone");
        String code = map.get("code");

        //2.对比验证码
        /*String smsCode = (String) session.getAttribute("SMS_CODE");
        if (StringUtils.isEmpty(code) || StringUtils.isEmpty(smsCode) || !StringUtils.equals(code, smsCode)) {
            throw new CustomException("验证码有误");
        }*/

        String codeFromRedis = (String) redisTemplate.opsForValue().get("SMS_CODE" + phone);
        if (StringUtils.isEmpty(code) || StringUtils.isEmpty(codeFromRedis) || !StringUtils.equals(code, codeFromRedis)) {
            throw new CustomException("验证码有误");
        }
        //3.查询手机号是否在数据库
        User user = userService.findByPhone(phone);

        if (user != null) {
            //4. 查询到了,代表的是一个老用户在登录,而且已经登录成功了
        } else {
            //5. 没查询到,代表一个新用户在注册,需要将用户信息保存到user表
            user = new User();
            user.setPhone(phone);
            userService.save(user);
        }
        //6. 将用户的信息保存到session中,这个名称必须用这个,因为拦截器中是根据这个名字获取的
        session.setAttribute("SESSION_USER", user);
        //7. 返回成功结果
        return ResultInfo.success(null);
    }

    //退出
    @PostMapping("/user/logout")
    public ResultInfo logout() {
        //1.销毁session
        session.invalidate();

        //2.返回成功标识
        return ResultInfo.success(null);
    }
}
